Skip to content

[3.14] gh-159136: Avoid stop-the-world when setting function name attributes (gh-159269) - #159726

Open
nascheme wants to merge 5 commits into
python:3.14from
nascheme:backport-010bebb-3.14
Open

nascheme wants to merge 5 commits into
python:3.14from
nascheme:backport-010bebb-3.14

Conversation

@nascheme

@nascheme nascheme commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Setting name, qualname, doc or module on a function stopped the world in the free-threaded build since gh-154826 and gh-154851, making functools.wraps() orders of magnitude slower with multiple threads.

Use the per-object critical section and _PyObject_XSetRefDelayed() instead, as gen_set_name() already does. Use acquire loads in the readers and hold strong references across error reporting, tracing, and eval-frame recording that may execute Python or detach the thread. The code, defaults, kwdefaults and closure setters still stop the world to clear func_version.

Document synchronization requirements for direct field access and the lifetime of borrowed function attributes.
(cherry picked from commit 010bebb)

…me attributes (pythongh-159269)

Setting __name__, __qualname__, __doc__ or __module__ on a function
stopped the world in the free-threaded build since pythongh-154826 and
pythongh-154851, making functools.wraps() orders of magnitude slower with
multiple threads.

Use the per-object critical section and _PyObject_XSetRefDelayed()
instead, as gen_set_name() already does.  Use acquire loads in the
readers and hold strong references across error reporting, tracing, and
eval-frame recording that may execute Python or detach the thread.  The
__code__, __defaults__, __kwdefaults__ and __closure__ setters still
stop the world to clear func_version.

Document synchronization requirements for direct field access and the
lifetime of borrowed function attributes.
(cherry picked from commit 010bebb)

Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
nascheme and others added 4 commits October 27, 2026 14:32
Resolve the committed conflicts using the 3.14 implementations and add
the missing _PyObject_XSetRefDelayed helper with its GIL-build fallback.
Move the tracing changes into ceval.c and remove the accidentally
imported main-branch ceval.h.

Keep the recorded function name alive while PyList_Append acquires its
lock. Add coverage for recording contention, tracing during attribute
replacement, and reentrant annotation finalizers.
@nascheme
nascheme marked this pull request as ready for review October 29, 2026 17:17
@nascheme
nascheme requested a review from markshannon as a code owner October 29, 2026 17:17
@nascheme nascheme added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Oct 29, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @nascheme for commit c866403 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F159726%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants